package main

import (
	"fmt"
	"os"

	"github.com/mattn/go-gtk/gdkpixbuf"
	"github.com/mattn/go-gtk/gtk"
)

func main() {
	gtk.Init(&os.Args) //环境初始化

	// fmt.Println(os.Args[0])

	builder := gtk.NewBuilder()          //新建builder
	builder.AddFromFile("loginui.glade") //读取glade文件

	//获取glade对应的控件
	window := gtk.WindowFromObject(builder.GetObject("window1")) //获取窗口控件

	buttonOk := gtk.ButtonFromObject(builder.GetObject("ok")) //按钮
	buttonNo := gtk.ButtonFromObject(builder.GetObject("no"))
	// labelName := gtk.LabelFromObject(builder.GetObject("name")) //标签
	// labelPass := gtk.LabelFromObject(builder.GetObject("password"))

	EntryName := gtk.EntryFromObject(builder.GetObject("entry1"))
	EntryPass := gtk.EntryFromObject(builder.GetObject("entry2"))

	imageFace := gtk.ImageFromObject(builder.GetObject("imageface"))
	//
	EntryPass.SetVisibility(false)
	//
	window.SetTitle("登陆窗口")
	window.SetIconFromFile("image/black.png")

	//
	buttonOk.SetLabel("YES")

	//获取image控件大小
	var w, h int
	imageFace.GetSizeRequest(&w, &h)

	//创建pixbuf，指定大小（宽度和高度），image有多大就设置多大
	//最后一个参数false代表不保存图片原来的尺寸
	pixbuf, _ := gdkpixbuf.NewPixbufFromFileAtScale("image/player.png", w, h, false)

	imageFace.SetFromPixbuf(pixbuf)

	//
	window.SetIcon(pixbuf)

	//pixbuf使用完毕，需要释放资源
	pixbuf.Unref()

	// fmt.Println("buttonno txt = ", buttonNo.GetLabel()) //获取按钮内容
	buttonNo.GetLabel()
	//
	buttonOk.Connect("clicked", func() {
		// fmt.Printf("you have inputted:\n name:%s\n password:%s\n", EntryName.GetText(), EntryPass.GetText())

		str := fmt.Sprintf("you have inputted:\n name:%s\n password:%s\n", EntryName.GetText(), EntryPass.GetText())

		//对话框展示输入内容
		dialog := gtk.NewMessageDialog(

			buttonOk.GetTopLevelAsWindow(),
			gtk.DIALOG_MODAL,
			gtk.MESSAGE_QUESTION,
			gtk.BUTTONS_YES_NO,
			"Are u Sure?\n"+str)
		dialog.SetTitle("tips")
		flag := dialog.Run() //运行对话框
		if flag == gtk.RESPONSE_YES {
			// fmt.Println("Pressed YES")
		} else if flag == gtk.RESPONSE_NO {
			// fmt.Println("Pressed No")
		} else {
			// fmt.Println("Pressed Closed")
		}
		dialog.Destroy() //销毁对话框

	})

	//信号处理，当用户在文本输入控件内部按回车键时引发activate信号
	EntryPass.Connect("activate", func() {
		fmt.Println("EntryPass text = ", EntryPass.GetText()) //获取内容
	})

	//按窗口关闭按钮，自动触发"destroy"信号
	window.Connect("destroy", gtk.MainQuit)

	window.Show()

	gtk.Main() //主事件循环，等待用户操作

}
